package top.lixb.libcommon.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import top.lixb.libcommon.R;
import top.lixb.libcommon.binding.BindingViewModel;

public abstract class RefreshableActivity<T extends ViewDataBinding> extends BaseActivity {

    protected SmartRefreshLayout mSrl;
    protected int mPage;
    protected ViewGroup mScrollView;
    protected T mRefreshContentBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_refresh);
        mSrl = (SmartRefreshLayout) findViewById(R.id.srl);
        mScrollView = (ViewGroup) findViewById(R.id.scrollroot);
        mSrl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData(true);
            }
        });
        mSrl.setEnableLoadMore(false);
        mRefreshContentBinding = DataBindingUtil.inflate(LayoutInflater.from(this), initRefreshLayout(), mScrollView, true);
        init();
        getData(true);
    }

    protected abstract void init();

    protected abstract int initRefreshLayout();




    protected abstract void getData(boolean refresh);

    protected void finishLoadMore() {
        if (mSrl.isEnableLoadMore()) {
            mSrl.finishLoadMore(2000);
        }
    }

    protected void finishGetData() {
        finishLoadMore();
        finishRefresh();
    }

    protected void finishRefresh() {
        if (mSrl.isEnableRefresh()) {
            mSrl.finishRefresh(2000);
        }
    }


}
